package com.fesine.crm.util;

import com.fesine.commons.util.DateUtils;
import com.fesine.dao.BasePo;
import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2017/10/17 14:39
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/10/17 14:39
 */
public class SystemUtils {

    /**
     * 获取项目根目录
     *
     * @return
     */
    public static String getRootPath() {
        return SystemUtils.class.getClassLoader().getResource("").getPath();
    }

    public static void setQueryMonth(BasePo po, String dateStr) {
        if (StringUtils.isNotBlank(dateStr)) {
            //2017-10-13 00:00:00
            //start 2017-10-01 00:00:00
            //end 2017-11-01 00:00:00
            Calendar calendar = Calendar.getInstance();
            try {
                calendar.setTime(DateUtils.formatString(dateStr));
            } catch (Exception e) {
                e.printStackTrace();
            }
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            Date firstDayOfMonth = calendar.getTime();
            calendar.add(Calendar.MONTH, 1);
            Date lastDayOfMonth = calendar.getTime();
            po.setQueryBeginDate(firstDayOfMonth);
            po.setQueryEndDate(lastDayOfMonth);

        }
    }

    public static void setQueryDate(BasePo po, String dateStr) {
        if (StringUtils.isNotBlank(dateStr)) {
            //2017-10-13 00:00:00
            //start 2017-10-13 00:00:00
            //end 2017-10-14 00:00:00
            Calendar calendar = Calendar.getInstance();
            try {
                calendar.setTime(DateUtils.formatString(dateStr));
            } catch (Exception e) {
                e.printStackTrace();
            }
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            Date firstDayOfMonth = calendar.getTime();
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            Date lastDayOfMonth = calendar.getTime();
            po.setQueryBeginDate(firstDayOfMonth);
            po.setQueryEndDate(lastDayOfMonth);

        }
    }

    public static int getWeekDay(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //一周第一天是否为星期天
        boolean isFirstSunday = (calendar.getFirstDayOfWeek() == Calendar.SUNDAY);
        //获取周几
        int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
        //若一周第一天为星期天，则-1
        if (isFirstSunday) {
            weekDay = weekDay - 1;
            if (weekDay == 0) {
                weekDay = 7;
            }
        }

        return weekDay;
    }

}
